<?php
/**
 * --------------------------------------------------------------------
 * 节点模型
 * --------------------------------------------------------------------
 * @author Midnight, yangyunzhou@foxmail.com
 * --------------------------------------------------------------------
 * @copyright Midnight Studio
 * --------------------------------------------------------------------
 */
class NodeModel extends RbacModel {

	/**
	 * 主键
	 * @var String
	 */
	public $pk = 'id';

	/**
	 * 字段
	 * @var Array
	 */
	public $fields = array('id','name','title','state','remark','sort','pid','level');

	/**
	 * --------------------------------------------
	 * 节点信息获取
	 * --------------------------------------------
	 * @author Midnight, yangyunzhou@foxmail.com
	 * @param Int $id
	 */
	public function nodeAction($id) {
		$val = array();
		$re = $this->get_data($id);
		if($id) { // 子节点
			$val['id'] = $re['id'];
			$val['level'] = ++$re['level'];
		} else { // 父节点
			$val['level'] = ($re['level']) ? $re['level'] : 1;
			$val['id'] = ($re['id']) ? $re['id'] : 0;
		}
		return $val;
	}

	/**
	 * --------------------------------------------
	 * 获取节点信息
	 * --------------------------------------------
	 * @author Midnight, yangyunzhou@foxmail.com
	 * @param $id 节点ID
	 */
	public function get_data($id) {
		return $this->find($id);
	}

	/**
	 * --------------------------------------------
	 * 获取指定子节点
	 * --------------------------------------------
	 * @author Midnight, yangyunzhou@foxmail.com
	 * @param $level 节点层级
	 * @param $id 指定节点
	 */
	public function children($level,$id=FALSE) {
		if($id) $id = "AND `pid`='$id'";
		return $this->where("`level`='$level'".$id)->select();
	}
}